package com.itheima.student.system08.entry;

import com.itheima.student.system08.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentEntry {

    private static ArrayList<Student> list = new ArrayList<>();
    private static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {


        loop:
        while (true) {
            System.out.println("----------欢迎使用学生管理系统----------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            System.out.println("请输入你的选择:");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findStudents();
                    break;
                case "5":
                    System.out.println("感谢您的使用!");
                    break loop;
                default:
                    System.out.println("您的输入有误,请重新输入!");
                    break;
            }
        }
    }


    // 添加学生
    public static void addStudent() {
        System.out.println("请输入你要输入的学生学号:");
        while (true) {
            String sid = sc.next();
            int index = getIndex(sid);
            if (index < 0) {
                Student student = getStudent(sid);
                list.add(student);
                System.out.println("添加成功!");
                break;
            } else {
                System.out.println("系统中已有此学号,请输入正确的学号!");
                break;
            }
        }
    }

    // 删除学生
    public static void deleteStudent() {
        if (list.size() == 0) {
            System.out.println("系统中没有学生信息,请先录入学生信息!");
        } else {
            System.out.println("请输入你要删除的学生的学号:");
            String sid = sc.next();
            int index = getIndex(sid);
            if (index >= 0) {
                list.remove(index);
                System.out.println("删除成功!");
            } else {
                System.out.println("系统中没有此学号信息,不需要删除!");
            }
        }


    }

    //  修改学生信息
    public static void updateStudent() {
        if (list.size() == 0) {
            System.out.println("系统中没有学生信息,请先录入学生信息!");
        } else {
            System.out.println("请输入你要删除的学生的学号:");
            String sid = sc.next();
            int index = getIndex(sid);
            if (index >= 0) {
                Student student = getStudent(sid);
                list.set(index, student);
                System.out.println("修改成功!");
            } else {
                System.out.println("系统中没有此学号信息,请先录入学生信息!");
            }
        }

    }

    //  查看学生
    public static void findStudents() {
        if (list.size() == 0) {
            System.out.println("系统中没有学生信息,请先录入学生信息!");
        } else {
            System.out.println("请先输入你的选择:");
            System.out.println("1.查看所有学生:");
            System.out.println("2.查看单独学生:");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    findAllStudents();
                    break;
                case "2":
                    IndividualStudents();
                    break;
                default:
                    System.out.println("您的选择输入错误,请重新输入:");
                    break;
            }
        }
    }

    //  查看所有学生信息
    private static void findAllStudents() {
        System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "性别" + "\t" + "生日");
        for (int i = 0; i < list.size(); i++) {
            // list.get(i)
            Student student = list.get(i);
            System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getGender() + "\t" + student.getBirthday());
        }

    }

    // 查看指定学生信息
    private static void IndividualStudents() {
        System.out.println("请输入你要查看的学生的学号:");
        String sid = sc.next();
        int index = getIndex(sid);
        if (index >= 0) {
            System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "性别" + "\t" + "生日");
            Student student = list.get(index);
            System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getGender() + "\t" + student.getBirthday());
        } else {
            System.out.println("系统中没有此学生,请先录入!");
        }

    }

    //  判断系统中是否已经存在该学号
    public static int getIndex(String sid) {
        for (int i = 0; i < list.size(); i++) {
            //  list.get(i)
            Student student = list.get(i);
            if (student.getSid().equals(sid)) {
                return i;
            }
        }
        return -1;
    }


    //  输入学生信息
    public static Student getStudent(String sid) {
        System.out.println("请输入你要输入的学生姓名:");
        String name = sc.next();
        System.out.println("请输入你要输入的学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入你要输入的学生性别:");
        String gender = sc.next();
        System.out.println("请输入你要输入的学生生日:");
        String birthday = sc.next();
        return new Student(sid, name, age, gender, birthday);
    }
}
